package com.liuhaoqi.raletiveblog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuhaoqi.raletiveblog.entity.Memory;
import com.liuhaoqi.raletiveblog.result.BlogPageResult;
import com.liuhaoqi.raletiveblog.result.WebRestResult;
import com.liuhaoqi.raletiveblog.result.business.PageResult.TimelinePageResult;
import com.liuhaoqi.raletiveblog.service.MemoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author liuhaoqi
 * @Date 2020/4/22 18:02
 * @Version 1.0
 */
@Controller
public class TimelineController {
    @Autowired
    MemoryService memoryService;

    @GetMapping("/timeline")
    public String getTimeline(Model model,
                              @RequestParam(value = "pageNum",defaultValue = "1") String pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "8") String pageSize){
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<Memory> memoryList = memoryService.getMemoryList();
        PageInfo<Memory> pageInfo = new PageInfo<>(memoryList);
        model.addAttribute("memorys",pageInfo);
        return "timeline";
    }

    @GetMapping("/admin/getMemmoryListMang")
    @ResponseBody
    public BlogPageResult<TimelinePageResult> getMemmoryListMang(@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                                                                 @RequestParam(defaultValue = "10",value = "pageSize") int pageSize) {
        int start =(pageNum-1)*pageSize;
        int end=pageSize;
        List<TimelinePageResult> memmoryListMang = memoryService.getMemmoryListMang(start, end, pageSize);
        BlogPageResult<TimelinePageResult> result = new BlogPageResult<>();
        result.setCount(memmoryListMang.get(0).getCnt());
        result.setCode(0);
        result.setMsg("查询所有时间线成功！");
        result.setData(memmoryListMang);
        return result;
    }

    @PostMapping("/admin/deleteMemoryByMemoryId")
    @ResponseBody
    public WebRestResult deleteMemoryByMemoryId(@RequestParam("memoryId") int memoryId) {
        WebRestResult result = new WebRestResult();
        int i = memoryService.deleteMemoryByMemoryId(memoryId);
        if (i > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除时间线成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除时间线失败！");
        }
        return result;
    }

    @PostMapping("/admin/updateMemoryByMemoryId")
    @ResponseBody
    public WebRestResult updateMemoryByMemoryId(@RequestBody Memory memory) {
        memory.setCreateTime(new Date());
        memory.setUpdateTime(new Date());
        int i = memoryService.updateMemoryByMemoryId(memory);
        WebRestResult result = new WebRestResult();
        if (i > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("更新成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新失败");
        }
        return result;
    }




}
